package com.botann.projects.base;

import com.botann.projects.constants.Code;
import com.botann.projects.constants.Constant;
import com.botann.projects.utils.HibernateUtils;
import com.botann.projects.utils.ModelUtils;
import com.sun.istack.internal.NotNull;
import org.hibernate.LockMode;
import org.hibernate.Session;
import org.hibernate.SessionFactory;
import org.hibernate.Transaction;
import org.hibernate.query.Query;
import org.springframework.beans.factory.annotation.Autowired;

import java.io.Serializable;
import java.lang.reflect.Method;
import java.util.List;

/**
 * Created by liushanguo on 2017/5/22.
 */
public abstract class BaseService  extends BaseImp{

    //************************************ 属性 *********************************************/

    @Autowired
    protected SessionFactory sessionFactory;
    protected Session currentSession = null;

    // 使用 偷梁换柱模式 切换数据源Session
    private Session realSession(){
        if (currentSession == null) return sessionFactory.getCurrentSession();
        return currentSession;
    }
    protected void useOtherDataSource(SessionFactory otherSessionFactory) {
        currentSession = otherSessionFactory.getCurrentSession();
    }

    public interface TransactionDo {
        void execute();
    }
    protected void useOtherDataSourceDoTransaction(SessionFactory otherSessionFactory, TransactionDo transactionDo) {
        useOtherDataSource(otherSessionFactory);
        Transaction tx = null;
        try {
            tx = otherSessionFactory.getCurrentSession().beginTransaction();
            transactionDo.execute();
            tx.commit();
        } catch (Exception e) {
            e.printStackTrace();
            if (tx != null)
                tx.rollback();
            throw new RuntimeException("数据更新到监控平台发生错误, 与本平台数据未同步: "+e.getMessage());
        } finally {
            useDefaultDataSource();
        }
    }


    protected void useDefaultDataSource() {
        currentSession = null;
    }



    //************************************ 异常 *********************************************/

    /**
     * Junit Test 专用
     * @param clazz Class
     * @param id Integer
     * @param <T> Class
     * @return T
     */
    public <T> T sessionLoadUser(Class<T> clazz,Integer id){
        return sessionLoad(clazz,id);
    }


    /**
     * 参数异常
     * @return SGRuntimeException
     */
    protected SGRuntimeException sgWrongParamRuntimeException(){
        return new SGRuntimeException(Code.PARAMETER_WRONG,"参数错误!");
    }


    /**
     * 业务错误
     * @param message   String异常信息
     * @return SGRuntimeException
     */
    protected SGRuntimeException sgErrorRuntimeException(String message){
        return new SGRuntimeException(Code.ERROR,message);
    }
    /**
     * 参数错误异常
     * @param message String异常信息
     * @return SGRuntimeException
     */
    protected SGRuntimeException sgWrongParamRuntimeException(String message){
        return new SGRuntimeException(Code.PARAMETER_WRONG,message);
    }
    /**
     * 运行异常
     * @param code Integer
     * @param message String
     * @return SGRuntimeException
     */
    protected SGRuntimeException sgRuntimeException(Integer code, String message){
        return new SGRuntimeException(code,message);
    }

    //*********************************** HQL Builder ******************************************/

    public static StringBuilder selectNewBuilder(Class clazz){
        return (new StringBuilder("select new ")).append(clazz.getName()).append("(");
    }

    protected StringBuilder modelFields(Class clazz, String alias){//包含父类属性,不包含扩展属性
        return ModelUtils.getFields(clazz,alias);
    }
    //包含父类属性,包含扩展属性
    protected StringBuilder modelFieldsE(Class clazz, String... alias){
        return ModelUtils.getFieldsE(clazz,alias);
    }

    protected StringBuilder modelFields(Class clazz, String[] alias, Boolean containsSuper,Boolean containExpand){
        return ModelUtils.getFields(clazz,alias,containsSuper,containExpand);
    }

    protected String like (String param) {
        if (param == null || param.isEmpty()) return null;
        param = param.trim();
        return "%"+param+"%";
    }
    protected String likePre (String param) {//1 加一个后缀
        if (param == null || param.isEmpty()) return null;
        return param+"%";
    }
    protected String likeEnd (String param) {
        if (param == null || param.isEmpty()) return null;
        return "%"+param;
    }

    protected static final String delCondition = "del=";
    protected String [] conditions(String... conditions) {
        String[] mConditions = new String[conditions.length];
        for (int i=0;i<conditions.length;i++){
            mConditions[i] = conditions[i];
        }
        return mConditions;
    }

    //************************************ DAO操作 *********************************************/

    protected void sessionLock() {
//        realSession().lock();
    }

    /**
     * 清除持久对象
     */
    protected void sessionFlushClear(){
        realSession().flush();
        realSession().clear();
    }

    protected void sessionFlush(){
        realSession().flush();
    }

    protected void sessionEvict(Object o) {
        realSession().evict(o);
    }

    protected void sessionClear(){
        realSession().clear();
    }

    /**
     * Save
     * 保存对象 保存成功对象 自动持有id
     * @param obj Entity
     * @return Serializable
     */
    protected Serializable sessionSave(Object obj){
        Serializable id = realSession().save(obj);
        return id;
    }

    /**
     * Delete
     * 伪删除 setDel
     * @param clazz Class
     * @param id id
     */
    protected void sessionDelete(Class clazz,Serializable id) {
        Object t = sessionLoad(clazz,id);
        try {
            Method m = clazz.getMethod("setDel",boolean.class);
            m.invoke(t,true);
            sessionUpdate(t);
        } catch (Exception e) {
            e.printStackTrace();
        }
    }

    protected void sessionDeleteForInt(Class clazz,Serializable id) {
        Object t = sessionLoad(clazz,id);
        try {
            Method m = clazz.getMethod("setDel",Integer.class);
            m.invoke(t,1);
            sessionUpdate(t);
        } catch (Exception e) {
            e.printStackTrace();
        }
    }

    /**
     * Delete
     * 删除
     * @param object Object
     */
    protected void sessionForceDelete(Object object) {
        realSession().delete(object);
    }
    protected void sessionForceDelete(Class clazz,Serializable id) {
        Object t = sessionLoad(clazz,id);
        sessionForceDelete(t);
    }
//    protected void sessionForceDelete(Class clazz, Object conditions, Object... params) {
//        StringBuilder hqlBuilder = new StringBuilder("delete from ");
//        hqlBuilder.append(clazz.getSimpleName());
//        if (conditions != null) {
//            hqlBuilder.append("where ");
//            if (conditions instanceof String) {
//                hqlBuilder.append(conditions);
//                hqlBuilder.append(" ?0 ");
//            } else {
//
//                for ()
//            }
//        }
//        sessionExecuteUpdate("delete from " + clazz.getSimpleName());
//    }

    /**
     * Update
     * 更新对象
     * @param obj Entity
     */
    protected void sessionUpdate(Object obj) {
        realSession().update(obj);
    }

    protected void sessionUpdate(String  hql) {
        realSession().createQuery(hql);
    }

    /**
     * Update
     * 保存或者更新对象,在不知道是否是new对象的情况使用
     * @param obj Entity
     */
    protected void sessionSaveOrUpdate(Object obj){
        realSession().saveOrUpdate(obj);
    }


    protected void sessionExecuteUpdate(String hql,Object... params) {
        HqlParams hqlParams = new HqlParams(hql,params);
        Session session = realSession();
        hql = hqlParams.getHql();
        Query query = session.createQuery(hqlParams.getHql());
        HibernateUtils.settingParameters(query,hqlParams.getParams());
        query.executeUpdate();
    }

    /**********start>> 单对象查询, 条件查询是立即加载的, id查询Get的立即加载的load是懒加载, Get can null, Load not null >>start***************/

    /**
     * Object
     * 立即加载对象, null able
     * @param clazz Entity Class
     * @param id id
     * @param <T> Entity
     * @return Entity initialized, null able
     */
    protected <T> T sessionGet(Class<T> clazz,Serializable id) {
        return realSession().get(clazz,id);
    }

    protected <T> T sessionGetForUpdate(Class<T> clazz,Serializable id) {
        return realSession().get(clazz,id,LockMode.PESSIMISTIC_WRITE);
    }

    /**
     * Object
     * 立即加载单个对象, not null
     * @param clazz Entity Class
     * @param id    id
     * @param <T>   Entity
     * @return Entity initialized, not null
     */
    protected <T> T sessionGetNN(Class<T> clazz,Serializable id) {
        T t = sessionGet(clazz,id);
        if (t==null) throw sgRuntimeException(Code.QUERY_NULL,"查询的对象不存在");
        return t;
    }
    protected <T> T sessionGetNNForUpdate(Class<T> clazz,Serializable id) {
        T t = sessionGetForUpdate(clazz,id);
        if (t==null) throw sgRuntimeException(Code.QUERY_NULL,"查询的对象不存在");
        return t;
    }

    /**
     * Object
     * 懒加载单个对象 not null, not initialized
     * @param clazz Class<T>
     * @param id    Serializable
     * @param <T> Entity
     * @return Entity not null, not initialized
     */
    protected <T> T sessionLoad(Class<T> clazz,Serializable id) {
        return realSession().load(clazz,id);
    }
    protected <T> T sessionLoadForUpdate(Class<T> clazz,Serializable id) {
        return realSession().load(clazz,id,LockMode.PESSIMISTIC_WRITE);
    }

    /**
     * Object
     * 条件立即加载单个对象 null able, must one (可空,必须仅有一个,否则抛异常) <=1
     * @param hql String
     * @param params Object or Object[]
     * @return Entity null able, must one
     */
    protected <T> T sessionGet(String hql, Object... params) {
        return sessionGet(true,false,hql,params);
    }
    protected <T> T sessionGetNN(String hql, Object... params) {
        return sessionGet(false,false,hql,params);
    }

    /**
     * Object
     * 条件立即加载单个对象 null able, must one (可空,不可多1个结果,否则抛异常) <=1
     * @param clazz Entity Class
     * @param conditions String or String[] 如: "id=" 或 {"id="}
     * @param params Object or Object[]
     * @return Entity null able, must one
     */
    protected <T> T sessionGet(Class<T> clazz,Object conditions, Object... params) {
        return sessionGet(true,false,clazz,null,conditions,params);
    }
    protected <T> T sessionGetNN(Class<T> clazz,Object conditions, Object... params) {
        return sessionGet(false,false,clazz,null,conditions,params);
    }

    /**
     * Object
     * 条件立即加载单个对象 null able, more one (可空,加载第一个) >=0
     * @param clazz Entity Class
     * @param conditions String or String[] 如: "id=" 或 {"id="}
     * @param params Object or Object[]
     * @return Entity null able, more one
     */
    protected <T> T sessionGetF(Class<T> clazz,Object conditions, Object... params) {
        return sessionGet(true,true,clazz,null,conditions,params);
    }
    protected <T> T sessionGetFNN(Class<T> clazz,Object conditions, Object... params) {
        return sessionGet(false,true,clazz,null,conditions,params);
    }
    protected <T> T sessionGetF(Class<T> clazz,Object orderBys,String[] conditions, Object... params) {
        return sessionGet(true,true,clazz,orderBys,conditions,params);
    }
    protected <T> T sessionGetFNN(Class<T> clazz,Object orderBys,String[] conditions, Object... params) {
        return sessionGet(false,true,clazz,orderBys,conditions,params);
    }


    /**
     * Object
     * 条件立即加载单个对象
     * @param canNull       可否为空
     * @param canMoreOne    是否可以多个结果
     * @param clazz         Class<T> clazz
     * @param conditions    String or String[] 如: "id=" 或 {"id="}
     * @param params        Object or Object[]
     * @param <T>           Entity
     * @return  Entity
     */
    protected <T> T sessionGet(Boolean canNull, Boolean canMoreOne, Class<T> clazz, Object orderBys, Object conditions, Object... params) {
        return sessionGet(canNull, canMoreOne, HibernateUtils.conditionsToHQL(clazz,conditions,orderBys),params);
    }

    protected <T> T sessionGet(Boolean canNull, Boolean canMoreOne, String hql, Object... params) {
        Pager pager = sessionQuery(0,1,hql,params);
        List list = pager.getList();
        if (list == null || list.size()==0) {
            if (!canNull) throw sgRuntimeException(Code.QUERY_NULL,"没有查到对象");
            return null;
        } else {
            if (!canMoreOne && list.size()>1) throw sgRuntimeException(Code.QUERY_GET_MORE_THAN_ONE,"查询结果多于1个");
            return (T)list.get(0);
        }
    }

    /**********end>> 单对象查询, 条件查询是立即加载的, id查询Get的立即加载的load是懒加载, Get can null, Load not null >>end***************/


    /**
     * Count
     * 简单查询对象的数量
     * @param clazz Class
     * @return long 数量
     */
    protected Serializable sessionCount(Class clazz){
        return sessionCount(clazz.getSimpleName(),null);
    }

    /**
     * Count
     * 简单查询对象的数量
     * @param clazz        Class
     * @param conditions   String or String[]
     * @param params        Object or Object[]
     * @return long
     */
    protected Serializable sessionCount(Class clazz,Object conditions, Object... params){
        return sessionCount(HibernateUtils.conditionsToHQL(clazz,conditions,null),params);
    }


    /**
     * Count
     * 公用查询列表
     * @param hql       String
     * @param params    List<Object> or String[] or common String or Object
     * @return 数量 Count
     */
    protected Serializable sessionCount(String hql,Object... params) {
        if (!hql.contains("count(*)")) {
            if (hql.contains("from")) hql = hql.substring(hql.indexOf("from")+4);// 从from后面获取查询hql
            hql = "select count(*) from "+ hql;
        }
//        hql = "select count(1) from ( " + hql+" ) countAll";
        return (Serializable)createQuery(hql,params).uniqueResult();
    }

    /**
     * List
     * 简单对象查询
     * @param clazz        Class
     * @param conditions   String or String[]
     * @param params       Object or Object[]
     * @return List
     */
    @Deprecated
    protected List sessionQueryL(Class clazz,Object conditions, Object... params) {
        return sessionQueryL(HibernateUtils.conditionsToHQL(clazz,conditions,null),params);
    }

    /**
     * List
     * 查询对象列表
     * @param clazz Class
     * @return List
     */
    @Deprecated
    protected List sessionQueryL(Class clazz) {
        return sessionQueryL(clazz, null, null, null);
    }

    /**
     * List
     * 查询对象列表
     * @param clazz     Class
     * @param orderBys  String[] or String 排序参数,如:"id" or "id desc"
     * @return List
     */
    @Deprecated
    protected List sessionQueryL(Class clazz, String... orderBys) {
        return sessionQueryL(clazz,orderBys,null,null);
    }

    /**
     * List
     * 查询对象列表
     * @param clazz         Class
     * @param conditions    String or String[] 如: "id=" 或 {"id="}
     * @param params        Object or Object[]
     * @return List
     */
    @Deprecated
    protected List sessionQueryL(Class clazz, String[] conditions, Object... params) {
        return sessionQueryL(clazz,null,conditions,params);
    }

    /**
     * List
     * 查询对象列表
     * @param clazz         Class                                            not null
     * @param orderBys      String[] or String 排序参数,如:"id" or "id desc"  null able
     * @param conditions    String or String[] 如: "id=" 或 {"id="}          null able
     * @param params        Object or Object[]                              null able
     * @return List
     */
    @Deprecated
    protected List sessionQueryL(Class clazz, Object orderBys, String[] conditions, Object...  params) {
        return sessionQueryL(HibernateUtils.conditionsToHQL(clazz,conditions,orderBys),params);
    }

    @Deprecated
    protected List sessionQueryL(String hql){
        return sessionQueryL(hql,null);
    }

    @Deprecated
    protected List sessionQueryL(String hql,Object... params) {
        return createQuery(hql,params).list();
    }

//    @Deprecated
//    protected List sessionQueryLOfLimit(Integer firstResult,Integer maxResult, String hql,Object... params) {
//        Query query = createQuery(hql,params);
//        query.setFirstResult(firstResult);
//        query.setMaxResults(maxResult);
//        return query.list();
//    }

    /**
    * Pager
    * 简单单对象分页查询
    * @param clazz         Class
    * @param pageIndex     Integer 从0开始
    * @param pageSize      Integer
    * @return Pager
    */
    @Deprecated
    protected Pager sessionQueryP(Class clazz,Integer pageIndex,Integer pageSize){
        return sessionQueryP(clazz,pageIndex,pageSize,null,null,null);
    }

    /**
     * Pager
     * 简单单对象分页查询
     * @param clazz         Class
     * @param orderBys      String[] or String 排序参数,如:"id" or "id desc"  null able
     * @param pageIndex     Integer 从0开始
     * @param pageSize      Integer
     * @return Pager
     */
    @Deprecated
    protected Pager sessionQueryP(Class clazz,Integer pageIndex,Integer pageSize,String... orderBys){
        return sessionQueryP(clazz,pageIndex,pageSize,orderBys,null,null);
    }
    /**
     * Pager
     * 简单单对象分页查询
     * @param clazz         Class
     * @param conditions    String or String[]
     * @param params        Object or Object[]
     * @param pageIndex     Integer 从0开始
     * @param pageSize      Integer
     * @return Pager
     */
    @Deprecated
    protected Pager sessionQueryP(Class clazz,Integer pageIndex,Integer pageSize,String[] conditions, Object... params){
        return sessionQueryP(clazz,pageIndex,pageSize,null,conditions,params);
    }
    /**
     * Pager
     * 简单单对象分页查询
     * @param clazz         Class
     * @param orderBys      String[] or String 排序参数,如:"id" or "id desc"  null able
     * @param conditions    String or String[]
     * @param params      Object or Object[]
     * @param pageIndex     Integer 从0开始
     * @param pageSize      Integer
     * @return Pager
     */
    @Deprecated
    protected Pager sessionQueryP(Class clazz,Integer pageIndex,Integer pageSize, Object orderBys, String[] conditions, Object... params){
        return sessionQueryP(pageIndex,pageSize,HibernateUtils.conditionsToHQL(clazz,conditions,orderBys),params);
    }

    /**
     * Pager
     * 公用查询分页列表
     * @param hql String
     * @param pageIndex Integer 从0开始,hibernate从0开始
     * @param pageSize Integer default 10
     * @param params   List<Object> or String[] or common String or Object
     * @return Pager
     */
    @Deprecated
    protected Pager sessionQueryP(Integer pageIndex, Integer pageSize, String hql, Object... params){
        if (pageIndex == null) pageIndex = Constant.PAGE_INDEX;
        if (pageSize == null || pageSize<0) pageSize = Constant.PAGE_SIZE;
        Query query = createQuery(hql,params);
        Pager pager = new Pager();
        Serializable total = query.list().size();
        pager.setTotal(total);
        query.setFirstResult(pageIndex * pageSize);
        query.setMaxResults(pageSize);
        List list = query.list();
        pager.setList(list);
        return pager;
    }


    /**
     * 一组新的接口自动判断返回Pager Or List
     */
    protected <T> T sessionQuery(Class clazz) {
        return sessionQuery(realSession(),clazz,null);
    }
    protected <T> T sessionQuery(Session session, Class clazz) {
        return sessionQuery(session,clazz,null);
    }
    protected <T> T sessionQuery(Class clazz, Object orderBys) {
        return sessionQuery(realSession(),clazz,orderBys,null,null);
    }
    protected <T> T sessionQuery(Session session, Class clazz, Object orderBys) {
        return sessionQuery(session,clazz,orderBys,null,null);
    }
    protected <T> T sessionQuery(Class clazz, Object conditions, Object... params) {
        return sessionQuery(realSession(),HibernateUtils.conditionsToHQL(clazz,conditions,null),params);
    }
    protected <T> T sessionQuery(Session session, Class clazz, Object conditions, Object... params) {
        return sessionQuery(session,HibernateUtils.conditionsToHQL(clazz,conditions,null),params);
    }
    protected <T> T sessionQuery(Class clazz, Object orderBys, String[] conditions, Object... params) {
        return sessionQuery(realSession(),clazz,null,orderBys,conditions,params);
    }
    protected <T> T sessionQuery(Session session, Class clazz, Object orderBys, String[] conditions, Object... params) {
        return sessionQuery(session,clazz,null,orderBys,conditions,params);
    }
    protected <T> T sessionQuery(Class clazz,Integer pageSize, Object orderBys, String[] conditions, Object... params){
        return sessionQuery(realSession(),clazz,null,pageSize,orderBys,conditions,params);
    }
    protected <T> T sessionQuery(Session session,Class clazz,Integer pageSize, Object orderBys, String[] conditions, Object... params){
        return sessionQuery(session,clazz,null,pageSize,orderBys,conditions,params);
    }
    protected <T> T sessionQuery(Class clazz, Integer pageIndex,Integer pageSizes) {
        return sessionQuery(clazz,pageIndex,pageSizes,conditions("1="),1);
    }
    protected <T> T sessionQuery(Class clazz,Integer pageIndex,Integer pageSize, String[] conditions, Object... params){
        return sessionQuery(realSession(),clazz,pageIndex,pageSize,null,conditions,params);
    }
    protected <T> T sessionQuery(Session session, Class clazz,Integer pageIndex,Integer pageSize, String[] conditions, Object... params){
        return sessionQuery(session,clazz,pageIndex,pageSize,null,conditions,params);
    }

    protected <T> T sessionQuery(Class clazz,Integer pageIndex,Integer pageSize, Object orderBys, String[] conditions, Object... params) {
        return sessionQuery(realSession(),clazz,pageIndex,pageSize,orderBys,conditions,params);
    }
    protected <T> T sessionQuery(Session session, Class clazz,Integer pageIndex,Integer pageSize, Object orderBys, String[] conditions, Object... params){
        return sessionQuery(session,pageIndex,pageSize,HibernateUtils.conditionsToHQL(clazz,conditions,orderBys),params);
    }
    protected <T> T sessionQuery(String hql, Object... params) {
        return sessionQuery(realSession(),hql,params);
    }
    protected <T> T sessionQuery(Session session, String hql, Object... params) {
        return sessionQuery(session,null,null,hql,params);
    }
    protected <T> T sessionQueryForSpecial(Integer pageIndex,Integer pageSize,String hql, Object... params) {
        return sessionQuery(pageIndex,pageSize,hql,params);
    }

    /**
     * Pager Or List
     * pageIndex!=null && pageSize!=null 都不为null 分页查return pager, 否则return list
     * @param pageIndex Integer     null able
     * @param pageSize  Integer     null able
     * @param hql       String      not null
     * @param params    Object...   null able
     * @return Pager Or List
     */
    protected <T> T sessionQuery(Integer pageIndex, Integer pageSize,  @NotNull String hql, Object... params){
        return sessionQuery(realSession(),pageIndex,pageSize,hql,params);
    }
    protected <T> T sessionQuery(Session session, Integer pageIndex, Integer pageSize,  @NotNull String hql, Object... params){
       return sessionQuery(session,pageIndex != null && pageSize != null,pageIndex,pageSize,hql,params);
    }

    /**
     *
     * * Pager Or List
     * 1. pageIndex 和 pageSize 都不为null 分页查return pager,
     * 2. pageSize!=null && pageIndex==null,return list max pageSize个,
     * 3. pageSize==null return list
     * @param pageAble  Boolean    是否返回Pager
     * @param pageIndex Integer     null able
     * @param pageSize  Integer     null able
     * @param hql       String      not null
     * @param params    Object...   null able
     * @return Pager Or List
     */
    protected <T> T sessionQuery(@NotNull Boolean pageAble, Integer pageIndex, Integer pageSize, @NotNull String hql, Object... params) {
        return sessionQuery(realSession(),pageAble,pageIndex,pageSize,hql,params);
    }
    protected <T> T sessionQuery(Session session, @NotNull Boolean pageAble, Integer pageIndex, Integer pageSize, @NotNull String hql, Object... params){
        Query query = createQuery(session,hql,params);
        Serializable total = 0;
//        if (pageAble) total = query.list().size();
        if (pageIndex == null && pageSize == null) {

        } else if (pageIndex == null) {
            query.setFirstResult(0);
            query.setMaxResults(pageSize);
        } else if (pageSize == null) {
            query.setFirstResult(pageIndex);
        } else {
            query.setFirstResult(pageIndex * pageSize);
            query.setMaxResults(pageSize);
        }
//        long time1 = System.currentTimeMillis();
//        long time = System.currentTimeMillis();
        List list = query.list();
//        System.out.println("时间--");
//        System.out.println(System.currentTimeMillis()-time);

        if (pageAble) {
            Pager pager = new Pager();
            total = sessionCount(hql,params);
//            total = 0;
//            System.out.println(System.currentTimeMillis()-time1);
            pager.setTotal(total);
            pager.setList(list);
            return (T)pager;
        } else  {
            return (T)list;
        }
    }

    /**
     * CreateQuery
     * @param hql       String    not null
     * @param params    Object... null able
     * @return Query
     */
    private Query createQuery(String hql, Object... params) {
        return createQuery(realSession(),hql,params);
    }
    private Query createQuery(Session session, String hql, Object... params) {
        HqlParams hqlParams = new HqlParams(hql,params);
        Query query = session.createQuery(hqlParams.getHql());
        HibernateUtils.settingParameters(query,hqlParams.getParams());
        return query;
    }

}
